<?php

namespace app\admin\model;

use think\Model;

class WechatCardPage extends Model
{
    // 表名
    protected $name = 'wechat_card_page';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'can_share_text',
        'create_time_text'
    ];

    public static function init()
    {
        self::event('before_insert', function (&$page) {
            $ids = $page->card_ids;
            $cardList = Db('WechatCard')->where('id','in', $ids)
                ->field('card_id,thumb_url')
                ->select();
            $page['card_list'] = json_encode($cardList,true);
        });
    }

    public function getCanShareList()
    {
        return ['true' => __('Can_share true'),'false' => __('Can_share false')];
    }     


    public function getCanShareTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['can_share'];
        $list = $this->getCanShareList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


}
